package com.objectoriented.tang.service;


import com.objectoriented.tang.DBpojo.History;
import com.objectoriented.tang.mapper.UserMapper;
import com.objectoriented.tang.WEBpojo.MessageResult;
import com.objectoriented.tang.DBpojo.User;
import com.objectoriented.tang.pojo.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserService {
    @Autowired(required=false)
    private UserMapper userMapper;
    public UserService(){}
    public List<User> getAllUser(){
        return userMapper.getAllUser();
    }

    public User getUser(HashMap<String, Object> map)
    {
        return userMapper.getUser(map);
    }
    public MessageResult ready(RequestUser requestUser)
    {
        Player player = new MatchThread().match(requestUser);
        HashMap<String,Object>map=new HashMap<String, Object>();
        map.put("UID",player.getUID());
        return new MessageResult(userMapper.getUser(map),player.getChessColor(),player.getRID());
    }

    public User register(HashMap<String, Object> map)
    {
        String id= (String) map.get("id");
        HashMap<String,Object> _map= new HashMap<String,Object>();
        _map.put("id",id);
        if(userMapper.login(_map)==null) {
            userMapper.register(map);
            System.out.println(userMapper.login(_map));
            return userMapper.login(_map);
        }
        return null;
    }

    public User login(HashMap<String, Object> map)
    {
        return userMapper.login(map);
    }

    public int cancel(int UID)
    {
       return new MatchQueue().cancel(UID);
    }

    public List<History> getHistory(HashMap<String, Object> map)
    {
        return userMapper.getHistory(map);
    }

    public int addHistory(HashMap<String, Object> map)
    {
        return userMapper.addHistory(map);
    }

    public int update(HashMap<String, Object> map)
    {
        return userMapper.update(map);
    }

}
